package com.zzdz.system.controller;

import com.zzdz.common.annotation.ControllerEndpoint;
import com.zzdz.common.controller.BaseController;
import com.zzdz.common.entity.QueryRequest;
import com.zzdz.common.entity.ZzdzConstant;
import com.zzdz.common.entity.ZzdzResponse;
import com.zzdz.common.utils.ZzdzUtil;
import com.zzdz.system.entity.DictItem;
import com.zzdz.system.service.IDictItemService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 字典-子表 Controller
 *
 * @author Zzdz
 * @date 2020-05-21 14:13:34
 */
@Slf4j
@Validated
@Controller
public class DictItemController extends BaseController {

    @Autowired
    private IDictItemService dictItemService;

    /**
    * 字典-子表列表视图
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dictItem")
    public String dictItemIndex(){
        return ZzdzUtil.view("system/dictItem/dictItemList.html");
    }

    /**
    * 获取所有字典-子表数据
    * @return
    */
    @GetMapping("dictItem")
    @ResponseBody
    @RequiresPermissions("dictItem:list")
    public ZzdzResponse getAllDictItems(DictItem dictItem) {
        return new ZzdzResponse().success().data(dictItemService.findDictItems(dictItem));
    }

    /**
    * 获取分页字典-子表数据
    * @return
    */
    @GetMapping("dictItem/list")
    @ResponseBody
    @RequiresPermissions("dictItem:list")
    public ZzdzResponse dictItemList(QueryRequest request, DictItem dictItem) {
        Map<String, Object> dataTable = getDataTable(this.dictItemService.findDictItems(request, dictItem));
        return new ZzdzResponse().success().data(dataTable);
    }

    /**
    * 新增字典-子表视图
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dictItem/add")
    @RequiresPermissions("dictItem:add")
        public String addDictItem() {
        return ZzdzUtil.view("system/dictItem/dictItemAdd.html");
    }

    /**
    * 保存字典-子表
    * @return
    */
    @ControllerEndpoint(operation = "新增DictItem", exceptionMessage = "新增DictItem失败")
    @PostMapping("dictItem")
    @ResponseBody
    @RequiresPermissions("dictItem:add")
    public ZzdzResponse saveDictItem(@Valid DictItem dictItem) {
        this.dictItemService.createDictItem(dictItem);
        return new ZzdzResponse().success();
    }

    /**
    * 编辑字典-子表视图
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dictItem/edit/{id}")
    @RequiresPermissions("dictItem:update")
    public String editDictItem(@PathVariable String id, Model model) {
        DictItem dictItem = dictItemService.getById(id);
        model.addAttribute(dictItem);
        return ZzdzUtil.view("system/dictItem/dictItemEdit.html");
    }
    /**
    * 更新字典-子表
    * @return
    */
    @ControllerEndpoint(operation = "修改DictItem", exceptionMessage = "修改DictItem失败")
    @PostMapping("dictItem/update")
    @ResponseBody
    @RequiresPermissions("dictItem:update")
    public ZzdzResponse updateDictItem(DictItem dictItem) {
        this.dictItemService.updateDictItem(dictItem);
        return new ZzdzResponse().success();
    }

    /**
    * 查看字典-子表
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dictItem/view/{id}")
    @RequiresPermissions("dictItem:list")
    public String dictItemView(@PathVariable String id, Model model) {
        DictItem dictItem = dictItemService.getById(id);
        model.addAttribute(dictItem);
        return ZzdzUtil.view("system/dictItem/dictItemView.html");
    }

    /**
    * 删除字典-子表
    * @return
    */
    @ControllerEndpoint(operation = "删除DictItem", exceptionMessage = "删除DictItem失败")
    @GetMapping("dictItem/delete")
    @ResponseBody
    @RequiresPermissions("dictItem:delete")
    public ZzdzResponse deleteDictItem(DictItem dictItem) {
        this.dictItemService.deleteDictItem(dictItem);
        return new ZzdzResponse().success();
    }

    /**
    * 删除字典-子表
    * @return
    */
    @ControllerEndpoint(operation = "删除DictItem", exceptionMessage = "删除DictItem失败")
    @GetMapping("dictItem/delete/{ids}")
    @ResponseBody
    @RequiresPermissions("dictItem:delete")
    public ZzdzResponse deleteDictItems(@PathVariable String ids) {
        this.dictItemService.deleteDictItemByIds(ids);
        return new ZzdzResponse().success();
    }

    /**
    * 导出字典-子表
    * @return
    */
    @ControllerEndpoint(operation = "修改DictItem", exceptionMessage = "导出Excel失败")
    @PostMapping("dictItem/excel")
    @ResponseBody
    @RequiresPermissions("dictItem:export")
    public void export(QueryRequest queryRequest, DictItem dictItem, HttpServletResponse response) {
        List<DictItem> dictItems = this.dictItemService.findDictItems(queryRequest, dictItem).getRecords();
        ExcelKit.$Export(DictItem.class, response).downXlsx(dictItems, false);
    }
}
